package com.trs.publish.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trs.publish.domain.Role;
import com.trs.publish.repository.RoleRepository;



/**
 * @author jia.haihong
 */
@Service
@Transactional
public class RoleService{

	@Autowired
    private RoleRepository roleRepository;

	public List<Role> getAllRoles() {
		return roleRepository.findRoles();
	}

	public List<Role> getList(Role role) {
		return roleRepository.findByRole(role);
	}

	public void deleteRole(long id) {
		roleRepository.deleteById(id);
	}

	public Role findById(long id) {
		return roleRepository.findById(id);
	}

	public long save(Role role) {
		return roleRepository.save(role);
	}

	public void update(Role role) {
		roleRepository.update(role);
	}

	public Role findByName(String rolename) {
		return roleRepository.findByName(rolename);
	}

	public String getUserPowers(String roles) {
		String authorities = "";
		List<Role> list =  roleRepository.findRoleAndPower();
		for(Role role:list){
			if(roles.contains(role.getRolename())){
				authorities += role.getPowername()+",";
			}
		}
		if(authorities.length()>0){
			authorities = authorities.substring(0, authorities.length()-1);
		}
		return authorities;
	}
}
